// pages/my/my.js
var validate = require('../../utils/validate.js');
var httpTool = require('../../utils/network.js');
var util = require('../../utils/util.js');
const app = getApp()
Page({
  data: {
    type: 0, // 0注册  1忘记密码
    nickName: '',
    username: '',
    password: '',
    code: '',
    pwdFlag: true,
    time: '',
    currentTime: 60,
    disabled: false
  },
  // 登录
  toLogin: function () {
    wx.navigateBack()
  },
  //提交
  toCommit: function () {
    if (this.data.type == 0) {
      this.toRegister();//注册提交
    } else {
      this.toResetPwd();//忘记密码提交
    }
  },
  //忘记密码提交
  toResetPwd: function () {
    console.log('toResetPwd')
    var username = this.data.username;
    var password = this.data.password;
    var code = this.data.code;
    if (!username) {
      util.fxAlert('请输入手机号')
      return;
    }
    if (!code) {
      util.fxAlert('请输入验证码')
      return;
    }
    if (!password) {
      util.fxAlert('请输入密码')
      return;
    }
    var regNum = new RegExp('^1[3-9][0-9]{9}$');
    if (!regNum.test(username)) {
      util.fxAlert('请输入正确的手机号')
      return;
    }
    var that = this;
    var param = {
      user: username,
      password: password,
      code: code,
    }
    var url = app.data.httpUrl + "v1/member/resetPassword";
    httpTool.sendRequest(
      url, param, '修改中...',
      function (res) {
        console.log(res)
        if (res.code == 200) {
          util.fxAlert('修改成功')
          wx.navigateBack()
        }
      },
      function (data) {
        util.fxAlert(data.msg)
      }
    )
  },
  //注册提交
  toRegister: function () {
    console.log('toRegister')
    var nickName = this.data.nickName;
    var username = this.data.username;
    var password = this.data.password;
    var code = this.data.code;
    if (!username) {
      util.fxAlert('请输入手机号')
      return;
    }
    if (!code) {
      util.fxAlert('请输入验证码')
      return;
    }
    if (!password) {
      util.fxAlert('请输入密码')
      return;
    }
    var regNum = new RegExp('^1[3-9][0-9]{9}$');
    if (!regNum.test(username)) {
      util.fxAlert('请输入正确的手机号')
      return;
    }
    var that = this;
    var param = {
      phone: username,
      userName: username,
      nickName: nickName,
      password: password,
      code: code,
    }
    var url = app.data.httpUrl + "v1/member/register";
    httpTool.sendRequest(
      url, param, '注册中...',
      function (res) {
        console.log(res)
        if (res.code == 200) {
          util.fxAlert('注册成功')
          wx.navigateBack()
        } else {
          util.fxAlert(res.msg)
        }
      },
      function (data) {
        util.fxAlert(data.msg)
      }
    )
  },

  
  // 输入
  inputListen: function (event) {
    var key = event.currentTarget.dataset.key;
    console.log(event.detail.value)
    this.data[key] = event.detail.value;
  },
  //密码显示切换
  showPwd: function () {
    this.setData({
      pwdFlag: !this.data.pwdFlag,
    })
  },
  //发送验证码
  sendSms: function () {
    console.log('sendSms')
    var username = this.data.username;
    if (!username) {
      util.fxAlert('请输入手机号')
      return;
    }
    var regNum = new RegExp('^1[3-9][0-9]{9}$');
    if (!regNum.test(username)) {
      util.fxAlert('请输入正确的手机号')
      return;
    }
    if (this.data.disabled) {
      return;
    }
    var that = this;
    // 0注册  1忘记密码
    var type = this.data.type == 0 ? 0 : 1
    var param = {
      phone: username,
      type: type,
      //saasId: app.globalData.saasId,
      //shopId: app.userData.shopId,
    }
    var url = app.data.httpUrl + "v1/sms/sendSmsCode";
    httpTool.sendRequest(
      url, param, '发送中...',
      function (res) {
        console.log(res)
        if (res.code == 200) {
          util.fxAlert('发送成功')
          that.setData({
            disabled: true,
          })
          that.getCode();
        }
      },
      function (e) {
        util.fxAlert(e.msg)
      }
    )
  },
  //验证码倒计时函数
  getCode: function () {
    var that = this;
    var currentTime = that.data.currentTime;
    that.setData({
      time: currentTime
    })
    var interval = setInterval(function () {
      that.setData({
        time: (currentTime - 1)
      })
      currentTime--;
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '',
          currentTime: 60,
          disabled: false
        })
      }
    }, 1000)
  },
  
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    var type = options.type ? options.type : 0;
    var phone = options.phone ? options.phone : '';
    this.setData({
      type: type,
      username: phone,
    })
    if (type == 0) {
      wx.setNavigationBarTitle({
        title: '注册',
      })
    } else {
      wx.setNavigationBarTitle({
        title: '忘记密码',
      })
    }
  },
  // 显示隐私条款点击事件
  showPrivacyPolicy: function () {
    util.navigateTo( '/pages/privacyPolicy/privacyPolicy')
  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成

  },
  onShow: function () {
    // 生命周期函数--监听页面显示

  },
  onHide: function () {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function () {
    // 生命周期函数--监听页面卸载

  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作

  },
  onReachBottom: function () {
    // 页面上拉触底事件的处理函数

  },
  onShareAppMessage: function () {
    // 用户点击右上角分享

  }
})
